package cx.ath.skyflyer.gpslog.io;

import j2meunit.framework.*;
import java.io.IOException;
import java.io.InputStream;

/**
 *
 * @author Ahti Legonkov
 */
public class LineReaderTest extends TestCase {

    public LineReaderTest() {
    }

    private LineReaderTest(String name, TestMethod method) {
        super(name, method);
    }

    public void setUp() {
    }

    public void tearDown() {
    }

    public Test suite() {
        TestSuite ts = new TestSuite();

        ts.addTest(new LineReaderTest("test1", new TestMethod() {
            public void run(TestCase tc) {
                ((LineReaderTest)tc).test1();
            }
        }));

        return ts;
    }

    private String join(String[] pieces, String glue) {
        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < pieces.length; ++i) {
            sb.append(pieces[i]).append(glue);
        }

        return sb.toString();
    }

    private void test1() {
        final String[] testData1 = new String[] {
            "test",
            "$GPGGA,084948.969,5859.0923,N,02443.4910,E,0,0,,81.6,M,20.1,M,,*74",
            "$GPGLL,5859.0923,N,02443.4910,E,084948.969,V,N*4A",
            "$GPRMC,084948.969,V,5859.0923,N,02443.4910,E,0.11,123.43,040709,,,N*70",
            "$GPVTG,123.43,T,,M,0.11,N,0.20,K,N*37",
            "$GPGGA,084949.769,5859.0923,N,02443.4909,E,0,0,,81.6,M,20.1,M,,*73",
            "$GPRMC,084949.769,V,5859.0923,N,02443.4909,E,0.11,123.43,040709,,,N*77",
            "$GPGGA,084951.000,5859.0923,N,02443.4909,E,0,0,,81.6,M,20.1,M,,*72",
        };

        final String testData = join(testData1, "\r\n");

        InputStream stream = new InputStream() {

            int pointer = 0;
                    
            public int read() throws IOException {
                if (pointer >= testData.length())
                    pointer = 0;

                return testData.charAt(pointer++);
            }

            public int available() throws IOException {
                return testData.length() - pointer;
            }
        };

        try {
            LineReader reader = new LineReader(stream);

            for (int i = 0; i < testData1.length; ++i) {
                final String test = reader.readString();
                final String expected = testData1[i];
                assertEquals(expected, test);
            }
        }
        catch (Exception e) {
            fail("Unexpected exception:"+e);
        }
    }
}
